$(function() {	

	// add the alternate css class to every second table row
	$("table tr:odd").addClass("alternate");
	
	// abillity to close info messages via mouse click
	$('.success:empty, .error:empty, .warning:empty, .paginator:empty').hide();	
	$(".success, .error, .warning").each(function() {
		$(this).click(function() {
			$(this).fadeOut();
		});
	});	
	
	// toggle admin sections via mouse click
	$(".header").each(function() {
		$(this).click(function() {
			$(this).next().slideToggle();
		});
	});
	
	// confirmation dialog
	$( "#dialog:ui-dialog" ).dialog("destroy");
	$( ".delete" ).click(function() {
		var a = this;
		$("#confirm").dialog({
			modal: true,
			height: 130,
			buttons: {
				"OK": function() {
					$( this ).dialog( "close" );
					window.location = a.href;
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			}
		});
		return false;
	});	
	$(".ui-dialog .ui-dialog-titlebar-close span").val("");	
	
	// check all checkboxes with a .delete-checkbox class
	var clicked = false;
	$(".check-all").click(function() {
		if (clicked == false) {
			$(".delete-checkbox").attr('checked', true);
			clicked = true;
		} else {
			$(".delete-checkbox").attr('checked', false);
			clicked = false;
		}
	});
	
	
	if ($("#action").val() != "edit") {
		$('#title').syncTranslit({destination:"slug"});
	}	
	
	// fancybox initialization
	$("a[rel='lightbox']").fancybox();
		
	// date picker
	$( ".datepicker" ).datepicker({dateFormat: 'yy-mm-dd'});
	
	// table filter
	var theTable = $('table.sortable')
	
	$("#filter").keyup(function() {
		$.uiTableFilter( theTable, this.value );
	})
	
	$('#filter-form').submit(function(){
		theTable.find("tbody > tr:visible > td:eq(1)").mousedown();
		return false;
	}).focus();

});